//
//  NetworkTools.swift
//  weibo
//
//  Created by 孙新 on 16/4/19.
//  Copyright © 2016年 孙新. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式
enum requestMethod:String{
    case Post = "post"
    case Get = "get"
}

class NetworkTools: AFHTTPSessionManager {
    
    //单例
    static let sharedTools: NetworkTools = {
        let tools = NetworkTools()
        //设置解析类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //定义请求
    typealias requestCallBack = (responseObject: AnyObject?, error:NSError?)->()
    
    //响应
    func request(method:requestMethod = .Get,urlString:String,parameters:AnyObject,finish:requestCallBack){
        //定义一个成功之后的闭包
        let success = {(dataTask:NSURLSessionDataTask,responseObject:AnyObject?)->Void in
            finish(responseObject: responseObject, error: nil)
            
        }
        
        //定义一个失败后的闭包
        let failure = {(dataTask:NSURLSessionDataTask?,error: NSError?)->Void in
            finish(responseObject: nil, error: error)
        }
        
        if method == .Get{
            GET(urlString, parameters: parameters, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, success: success, failure: failure)
        }
        
    }
    
    

}
